//
//  NongChaoViewController.m
//  NC-Trendy
//
//  Created by brother on 16/2/23.
//  Copyright © 2016年 liang. All rights reserved.
//
#import "NongChaoViewController.h"

@interface NongChaoViewController ()<UIScrollViewDelegate, ChaoZhuoShowArticleDelegate>

@property (strong, nonatomic) UIScrollView *myScrollView;

//状态view的属性
@property (weak, nonatomic) IBOutlet UIView *stateView;
//stateView约束的X的属性
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *stateXConstraints;
//定义button点击的索引
@property (assign, nonatomic) NSInteger selectedIndex;

@property (weak, nonatomic) IBOutlet UIButton *wenhuaBtn;

@property (nonatomic, strong) NSArray *typeArray;

@end

@implementation NongChaoViewController



- (NSArray *)typeArray {
    if (_typeArray == nil) {
        _typeArray = @[ChaoZhuo,MingXing,ShuMa,Meishi,WenHua];
    }
    return _typeArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectedIndex = 0;
    //设置滚动视图
    [self setScrollView];
    self.view.backgroundColor = [UIColor blackColor];
  //  NSLog(@"%@",NSStringFromCGRect(self.myScrollView.frame));
    // Do any additional setup after loading the view.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)viewDidAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    //找出view上所有的子view
    for (UIView *obj in self.view.subviews) {
        //判断找出button
        if ([obj isKindOfClass:[UIButton class]]) {
            //将obj对象转为UIbutton属性
            UIButton *btn = (UIButton *)obj;
            //初始化将所有的button的字体颜色设置成黑色
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            //判断找出当前的button
            if (btn.tag == self.selectedIndex + 10) {
                //设置当前的button字体的颜色为蓝色
                [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
                //按照不同的button获得当前的selectedIndex
                self.selectedIndex = btn.tag - 10;
                //改变stateView约束的X的属性，初始-20 + 不同button的x的值 * selectedIndex
                self.stateXConstraints.constant = -20 + (self.view.bounds.size.width / 5) * self.selectedIndex;
            }
        }
    }
}

#pragma mark 将改变stateView约束的X的属性和改变button字体颜色的方法抽出来，做成联动
- (void)changeStateView:(NSInteger)selectedIndex {
    
    //改变stateView约束的X的属性，初始-20 + 不同button的x的值 * selectedIndex
    self.stateXConstraints.constant = -20 + (self.view.bounds.size.width / 5) * self.selectedIndex;
    //设置除了当前的button字体的颜色为黑色
    for (UIView *obj in self.view.subviews) {
        if ([obj isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)obj;
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            if (btn.tag == self.selectedIndex + 10) {
                //设置当前的button的字体颜色为蓝色
                [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            }
        }
    }

}
#pragma mark 标签栏的设置和联动
- (IBAction)chooseLeaveNoteState:(UIButton *)sender {
    //按照不同的button获得当前的selectedIndex
    self.selectedIndex = sender.tag-10;
    [self changeStateView:self.selectedIndex];
    CGFloat x = self.myScrollView.frame.size.width * self.selectedIndex;
    //动画显示用户界面
    [UIView animateWithDuration:0.4f animations:^{
        self.myScrollView.contentOffset = CGPointMake(x, 0);
    }];
    
}

#pragma mark设置滚动视图
-(void)setScrollView {
    self.myScrollView = [[UIScrollView alloc] init];
    self.myScrollView.delegate = self;
    self.myScrollView.pagingEnabled = YES;
    CGFloat scrollViewY = CGRectGetMaxY(self.stateView.frame);
    CGFloat scrollViewH = MainScreenHeight - scrollViewY;
    [self.myScrollView setFrame:CGRectMake(0, scrollViewY, MainScreenWidth, scrollViewH)];
    //设置滚动视图属性
    self.myScrollView.contentSize = CGSizeMake(MainScreenWidth*5, self.myScrollView.height);
    
    CGFloat x = 0;
    CGFloat y = 0;
        
    [self.typeArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        ChaoZhuoViewController *viewContro = [[ChaoZhuoViewController alloc]initWithType:obj];
        viewContro.delegate = self;
        viewContro.view.frame = CGRectMake(x + (idx*MainScreenWidth), y, kScreenW, self.myScrollView.height);
        NSLog(@"%f",self.myScrollView.height);
        NSLog(@"screen%f",MainScreenHeight);
       // viewContro.view.backgroundColor = [UIColor greenColor];
        [self.myScrollView addSubview:viewContro.view];

    }];
    
    //将滚动视图添加进视图中
    [self.view addSubview:self.myScrollView];
}

#pragma mark - <UIScrollViewDelegate>代理方法
//滚动视图时触发的方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //滚动视图的宽
    CGFloat width = scrollView.frame.size.width;
    
    //当前显示第几个视图
    NSInteger number = (int)scrollView.contentOffset.x / width;
    //获取与当前控制器对应的标签栏按钮的索引
    self.selectedIndex = number;
    //实现联动功能
    [self changeStateView:self.selectedIndex];
}

- (void)showArticleWithId:(NSString *)a_id {
    WebViewController *webView = [[WebViewController alloc]init];
    webView.artcle_id = a_id;
    [self.navigationController pushViewController:webView animated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
